package com.atguigu.mq.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MQProducerAckConfig implements RabbitTemplate.ConfirmCallback , RabbitTemplate.ReturnsCallback{

@Autowired
private RabbitTemplate rabbitTemplate;

@PostConstruct
public void init(){
    rabbitTemplate.setConfirmCallback(this);
    rabbitTemplate.setReturnsCallback(this);
}

//这个是生产到交换机的
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack){
            log.info("消息发送到交换机没问题" + correlationData);
        }else {
            log.info("没有发送到交换机"+ correlationData +"原因是"+cause);
        }
    }


    //这是交换机到队列的
    @Override
    public void returnedMessage(ReturnedMessage returned) {
        log.info("消息主体" + new String(returned.getMessage().getBody()));
        log.info("响应码是" + returned.getReplyCode());
        log.info("描述" + returned.getReplyText());
        log.info("使用的交换机" + returned.getExchange());
        log.info("消息使用的路由键" + returned.getRoutingKey());
    }
}